<?php
/**
 * Ora - 分类管理模块
 * @author lan7
 * @copyright lan7.net
 * @version 2.0
 */

if(!defined('IN_ORA')) {
	exit('Access Denied');
}
class admin_cat extends mod{

	/**
	 * 模块初始化
	 *
	 * @return admin_cat
	 */
	function admin_cat(){
		parent::__construct();
		session_start();
		$this->setTplDir(PUBLIC_PATH);
		$this->setMsgTpl("admin/msg.html");
		if(!isset($_SESSION[$GLOBALS['ora_cfg']['sess_prefix']."admin"])){
			$this->showMsg("请先登录!",parse_uri("common-login"),1);
		}
		import("lib.cat");
	}

	/**
	 * 分类列表
	 *
	 */
	function index(){
		$this->assign('cats',cat_lib::getSubChild(0));
		$this->display("admin/cat.html");
	}

	/**
	 * 新增分类
	 *
	 */
	function add(){
		$pid = is_numeric($GLOBALS['_param']['pid'])?$GLOBALS['_param']['pid']:0;
		$id = is_numeric($GLOBALS['_param']['id'])?$GLOBALS['_param']['id']:0;
		if($id){
			$cat = $GLOBALS['db']->getOne("select * from ".tn('cat')." where Id = $id");
			$pid = $cat['pid'];
		}
		$this->assign('cat',$cat);
		$this->assign('pid',$pid);
		$this->assign('cats',cat_lib::getSubChild(0,0));
		$this->display("admin/cat.html");
	}

	/**
	 * 修改分类
	 *
	 */
	function modify(){
		$this->add();
	}

	/**
	 * 执行新增或更新分类数据
	 *
	 */
	function save(){
		$cat_id = is_numeric($_POST['cat_id'])?$_POST['cat_id']:0;
		$cat_name = $_POST['cat_name'];
		$pid = is_numeric($_POST['pid'])?$_POST['pid']:0;
		$pic = $_POST['pic'];
		if(is_file(UPLOAD_PATH.$pic)){
			@io::remove(UPLOAD_PATH.$pic);
			@io::remove(UPLOAD_PATH.timg($pic));
		}
		$cat_type = $_POST['cat_type']==1?1:0;
		if($cat_name == ''){
			$this->showMsg('请输入类别名称!',$_SERVER['HTTP_REFERER'],1);
		}
		import("lib.upload");
		$up = new Upload("jpg|gif|png");
		if(!$up->errInfo){
			$pic = $up->files[0]["urlPath"];
			import("lib.image");
			image::thumb(UPLOAD_PATH.$pic,'',$GLOBALS['ora_cfg']['thumb_width'],$GLOBALS['ora_cfg']['thumb_height']);
		}
		if($pid > 0){
			$cat_path = $GLOBALS['db']->getOne("select * from ".tn('cat')." where id = $pid");
			$cat_path = $cat_path['path'];
			$curr_path = $cat_path."-".$pid;
		}else{
			$curr_path = '0';
		}
		$sql = "insert into ".tn('cat')."(cat_name,cat_type,pid,pic,path) values('$cat_name',$cat_type,$pid,'$pic','$curr_path')";
		if($cat_id > 0){
			$sql = "update ".tn('cat')." set path='$curr_path',cat_name = '$cat_name' , cat_type = $cat_type , pid = $pid , pic = '$pic' where Id = $cat_id";
		}
		if($this->db->execute($sql)){
			$this->showMsg('操作成功!',parse_uri('admin_cat-index'),1);
		}else{
			$this->showMsg('操作失败!',parse_uri('admin_cat-index'),1);
		}
	}

	/**
	 * 删除分类
	 *
	 */
	function del(){
		$id = $GLOBALS['_param']['id'];
		if(!is_numeric($id)){
			$this->showMsg("非法参数!");
		}
		$ids = cat_lib::getCatIdStr($id);
		$cat = $this->db->getOne("select * from ".tn('cat')." where Id = $id");
		$text = "文章动态";
		if($cat['cat_type'] === 1){
			$sql = "select count(*) from ".tn('product')." where cat_id in ($ids)";
			$text= "产品";
		}
		$sql = "select count(*) from ".tn('article')." where cat_id in ($ids)";
		if($GLOBALS['db']->count($sql)){
			$this->showMsg('请先删除分类下属'.$text.'!',parse_uri('admin_cat-index'),1);
		}
		$this->db->execute("delete from ".tn('cat')." where pid = $id or id = $id");
		$this->showMsg('操作成功!',parse_uri('admin_cat-index'),1);
	}


}

?>